package com.mima.minaserver.service;

import org.apache.mina.core.session.IoSession;

import com.mima.minaserver.repository.CameraRepository;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

public class RabbitMQMessageProcess implements Runnable{

	private Cache cache;
	private IotServerMessage iotServerMessage;
	private CameraRepository cameraRepository;
	private String uuid;
	private int on_or_off;
	private int channel;
	public RabbitMQMessageProcess(IotServerMessage iotServerMessage,CameraRepository cameraRepository,Cache cache,String uuid,int on_or_off,int channel) {
		this.cache = cache;
		this.iotServerMessage = iotServerMessage;
		this.cameraRepository = cameraRepository;
		this.uuid = uuid;
		this.on_or_off = on_or_off;
		this.channel = channel;
	}
	@Override
	public void run() {
		Element element = cache.get(uuid);
		if (element ==null)return;
		IoSession session = (IoSession)element.getObjectValue();
		iotServerMessage.sendDeviceOnOFFMessage(session, on_or_off,channel);
		cameraRepository.updateOpened(channel, uuid);
	}

}
